package com.web;

import com.bean.Menu;
import com.bean.Role;
import com.bean.Users;
import com.dao.impl.RolesDaoImpl;
import com.service.MenuService;
import com.service.RolesService;
import com.service.impl.MenuServiceImpl;
import com.service.impl.RolesServiceImpl;
import com.util.PageUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(urlPatterns = "/power/role/roles")
public class RoleServlet extends HttpServlet {
    private RolesService  rolesService = new RolesServiceImpl();
    private MenuService menuService = new MenuServiceImpl();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        if("select".equals(method)){
            select(req,resp);
        }else if("selectmenus".equals(method)){
            selectmenus(req,resp);
        }else if("insert".equals(method)){
            insert(req,resp);
        }else if("findbyid".equals(method)){
            findbyid(req,resp);
        } else if ("update".equals(method)) {
            update(req,resp);
        } else if ("delete".equals(method)) {
            delete(req,resp);
        }
    }
//删除角色
    private void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        String roleId = req.getParameter("roleId");
        int i = rolesService.delete(Integer.parseInt(roleId));
        if(i>0){
            req.getRequestDispatcher("/power/role/roles?method=select").forward(req,resp);
        }else {
            req.getRequestDispatcher("/power/role/roles?method=select").forward(req,resp);
        }
    }

    //添加角色
    protected void insert(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String rolename = req.getParameter("rolename");
        String state = req.getParameter("state");
        String[] menuids = req.getParameterValues("menuid");
        int i = rolesService.insertRole(rolename, state, menuids);
        if(i>0){
            resp.sendRedirect("/power/role/roles?method=select");
        }else {
            resp.sendRedirect("/power/role/roles?method=select");
        }


    }
    //查询菜单列表
    protected void selectmenus(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
          //1.接收参数
          //2.调取service方法
        List<Menu> menuList = menuService.getMenuList();
        req.setAttribute("menulist",menuList);
        req.getRequestDispatcher("add.jsp").forward(req,resp);
    }
    protected void select(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //接收参数
        String index = req.getParameter("index");
        int pageIndex=(index==null||index.length()==0)?1:Integer.parseInt(index);
        //读取service方法(1.查询数据列表的方法 2.查询总条数的方法)
        PageUtil pageUtil = new PageUtil();
        List<Role> rolesList = rolesService.getRolesList(pageIndex, pageUtil.getPageSize());
        int total = rolesService.total();
        pageUtil.setTotal(total);
        pageUtil.setPageIndex(pageIndex);
        pageUtil.setDataList(rolesList);

        //存值跳页面
        req.setAttribute("pi",pageUtil);
        req.getRequestDispatcher("list.jsp").forward(req,resp);
    }
    //主表查询
    protected void findbyid(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取角色信息
        String roleId = req.getParameter("roleId");
        Role role = rolesService.findbyid(Integer.parseInt(roleId));
        //获取菜单信息
        List<Menu> menuList = menuService.getMenuList();
        req.setAttribute("menulist",menuList);
        req.setAttribute("role",role);
        req.getRequestDispatcher("edit.jsp").forward(req,resp);
    }
    protected void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        String roleId = req.getParameter("roleId");
        String roleName = req.getParameter("rolename");
        String roleState = req.getParameter("state");
        String[] menuids = req.getParameterValues("menuid");
        int i = rolesService.updateRole(Integer.parseInt(roleId),roleName,Integer.parseInt(roleState), menuids);
        if(i>0){
            req.getRequestDispatcher("/power/role/roles?method=select").forward(req,resp);
        }else {
            req.getRequestDispatcher("/power/role/roles?method=select").forward(req,resp);
        }
    }


}
